<?php
/**
 * @version 	$Id: fRequest.class.php 84 2010-03-18 00:06:10Z mylifeisskidrow@gmail.com $
 * @author 		$Author:Progaramer mylifeisskidrow@gmail.com $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */

defined('IN_FLYWHEEL') or die('Restricted Access');

/**
 * Request
 * 	Request quan ly va luu tru cac thong tin tu HTTP Request
 * 	
 * @package 	system
 * @subpackage 	environment
 *
 */
class fRequest {
	const POST = 'POST';
	const GET = 'GET';

	protected $method;
	private $getVars = array();	
	
	public function __construct() {
		$this->method = $_SERVER['REQUEST_METHOD'];					
	}
	
	/**
	 * Get Method
	 *
	 * @return string
	 */
	public function getMethod() {
		return $this->method;
	}
	
	/**
	 * Retrieves the uniform resource identifier for the current web request.
	 *
	 * @return string Unified resource identifier
	 */
	public function getUri() {
		$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
		
		return $this->isAbsUri() ? $uri : $this->getUriPrefix().$uri;		
	}
	
	/**
	 * See if the client is using absolute uri
	 *
	 * @return boolean true, if is absolute uri otherwise false
	 */
	public function isAbsUri()
	{
	  return isset($_SERVER['REQUEST_URI']) ? preg_match('/^http/', $_SERVER['REQUEST_URI']) : false;
	}
	
	/**
	 * Returns Uri prefix, including protocol, hostname and server port.
	 *
	 * @return string Uniform resource identifier prefix
	 */
	public function getUriPrefix()
	{	  
		if ($this->isSecure())
		{
		  $standardPort = '443';
		  $protocol = 'https';
		}
		else
		{
		  $standardPort = '80';
		  $protocol = 'http';
		}
		
		$host = explode(':', $_SERVER['HTTP_HOST']);
		if (count($host) == 1)
		{
			$host[] = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : '';
		}
		
		if ($host[1] == $standardPort || empty($host[1]))
		{
			unset($host[1]);
		}
		
		return $protocol.'://'.implode(':', $host);
	}
	
	/**
	 * Returns true if the current request is secure (HTTPS protocol).
	 *
	 * @return boolean
	 */
	public function isSecure()
	{
		return (
		  (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == 1))
		  ||
		  (isset($_SERVER['HTTP_SSL_HTTPS']) && (strtolower($_SERVER['HTTP_SSL_HTTPS']) == 'on' || $_SERVER['HTTP_SSL_HTTPS'] == 1))
		  ||
		  (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https')
		);
	}
	
	/**
	 * Returns true if the request is a XMLHttpRequest.
	 *
	 * It works if your JavaScript library set an X-Requested-With HTTP header.
	 * Works with Prototype, Mootools, jQuery, and perhaps others.
	 *
	 * @return bool true if the request is an XMLHttpRequest, false otherwise
	 */
	public function isXmlHttpRequest() {
		return ($this->getHttpHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
	}
	
	public function getHttpHeader($name, $prefix = 'http') {
	  if ($prefix) {
		$prefix = strtoupper($prefix).'_';
	  }
	
	  $name = $prefix.strtoupper(strtr($name, '-', '_'));
	  return isset($_SERVER[$name]) ? $this->stripSlashes($_SERVER[$name]) : null;
	}
	
	/**
	 * Strip Slashes	 
	 * 	
	 * @param mixed data 		Data to be processed
	 * @return mixed 			Processed data
	 */
	public function stripSlashes(&$data)
	{
		return is_array($data)?array_map(array($this,'stripSlashes'),$data):stripslashes($data);
	}
	
	/**
	 * Post
	 *
	 * @param string $name
	 * @param int $type
	 * @param mixed $default
	 */
	public function post($name, $type = fFilter::TYPE_STRING, $default = null) {
		return $this->get($name, $type, $default, 'POST');
	}
	
	/**
	 * Get
	 *
	 * @param string $name 		Variable name
	 * @param int $type 		Kieu du lieu tra ve type for the variable, for valid values see {@link Filter::clean()}
	 * @param mixed $default	Default value if value not exist
	 * @param string $method	Method	
	 * @return mixed
	 */
	public function get($name , $type = fFilter::TYPE_STRING, $default = null, $method = 'GET') {		
		$method = strtoupper($method);		
		switch ($method) {
			case 'GET'	:
				if (isset($this->getVars[$name]) && $this->getVars[$name] !== null) 
					return $this->getVars[$name];
				$var = $_GET[$name];
				break;
			case 'POST' :
				$var = $_POST[$name];				
				break;
			case 'FILES'	:
				$var = $_FILES[$name];
				break;
			case 'COOKIE'	:
				$var = $_COOKIE[$name];				
				break;
			case 'ENV'	:
				$var = $_ENV[$name];
				break;
			case 'SERVER':
				$var = $_SERVER[$name];
				break;
			default:
				$var = $_REQUEST[$name];
				break;	
		}
		if ($var === null) $var = $default; 
		$var = fFilter::clean($var, $type);
			
		if (get_magic_quotes_gpc() && $method != 'FILES') {
			$this->stripSlashes($var);
		}
		if ($method == 'GET' && $var !== null) $this->getVars[$name] = $var;		
		return $var;
	}	
	
	/**
	 * Redirect
	 *
	 * @param string $url
	 * @param int $code the HTTP status code. 
	 * 	Defaults to 302. See {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html} 
	 * @return void
	 */
	public static function redirect($url, $code = 302) {
		if (strpos($url, 'http') === false) { //not entire url
			if (strpos($url, '/') === 0) $url = substr($url, 1, strlen($url));
			$url = fRouter::getInstance()->getBaseUrl() . $url;
		}
		header('Location: '.$url, true, $code);
	}
	
	/**
	 * Check Token 
	 *
	 * @param method $method
	 */
	public static function checkToken($method = fRequest::POST) {
		$token = fFactory::getSession()->get('token');
		$value = fFactory::getRequest()->get($token, fFilter::TYPE_ALNUM, null, $method);		
		if ($value === '1') {
			return true;					
		}
		
		return false;
	}
}